<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-input size="default" placeholder="请输入设备编号" style="max-width: 280px" v-model="code"> </el-input>
				激活状态：
				<el-select size="default" v-model="status" style="width: 150px">
					<el-option label="全部" value=""></el-option>
					<el-option label="已激活" value="actived">已激活</el-option>
					<el-option label="未激活" value="disactived">未激活</el-option>
				</el-select>
				代理商:
				<el-select size="default" v-model="dealerId" style="width: 300px" clearable>
					<el-option v-for="d in dealerList" :key="d.id" :label="d.orgName" :value="d.id">{{ d.orgName }}</el-option>
				</el-select>

				<el-button size="default" type="primary" class="ml10" @click="searchList">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddDevice('add')" v-auth="'COMPANY'">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增设备
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="code" label="设备编号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="equipmentmode" label="型号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag v-if="scope.row.status == 'actived'" type="success">已激活</el-tag>
						<el-tag v-if="scope.row.status == 'disactived'" type="danger">未激活</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="createdAt" label="录入时间" show-overflow-tooltip></el-table-column>
				<!-- <el-table-column prop="operator" label="操作人" show-overflow-tooltip></el-table-column> -->
				<el-table-column prop="dealer.orgName" label="代理商" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="130">
					<template #default="scope">
						<el-button v-if="manageable" size="small" text type="primary" @click="onOpenEditDevice(scope.row)">修改</el-button>
						<el-button v-if="manageable" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
						<el-button v-if="manageable" size="small" text type="primary" @click="onUnbound(scope.row)">解绑</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.pageNum"
				background
				v-model:page-size="state.tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<deviceDialog ref="deviceDialogRef" @refresh="getTableData()" />
		<deviceEditDialog ref="deviceEditDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemUser">
import { defineAsyncComponent, reactive, onMounted, ref, computed } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';

import { useDevice } from './device';
import { useUserInfo } from '/@/stores/userInfo';
import { storeToRefs } from 'pinia';
const deviceStore = useDevice();
const userStore = useUserInfo();
const { userInfo } = storeToRefs(userStore);
// 引入组件
const deviceDialog = defineAsyncComponent(() => import('/@/views/device/device/dialog.vue'));
const deviceEditDialog = defineAsyncComponent(() => import('/@/views/device/device/editDialog.vue'));

// 定义变量内容
const deviceDialogRef = ref();
const deviceEditDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			pageNum: 1,
			pageSize: 10,
		},
	},
});
const dealerList = ref<any>([]);
const code = ref('');
const status = ref('');
const dealerId = ref('');
const manageable = computed(() => {
	if (userInfo.value?.role) {
		return userInfo.value?.role?.indexOf('COMPANY_ADMIN') >= 0 || userInfo.value?.role?.indexOf('DEALER_ADMIN') >= 0;
	}
	return false;
});
const loadDealers = () => {
	// if (userInfo.value?.role && userInfo.value?.role.indexOf('COMPANY_AD') >= 0) {
	deviceStore.getDealerList().then((res) => {
		dealerList.value = res;
	});
	// }
};
// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	deviceStore
		.getDeviceList(code.value, status.value, dealerId.value, state.tableData.param.pageSize, state.tableData.param.pageNum)
		.then((res) => {
			state.tableData.data = res.list;
			state.tableData.total = res.total;
			state.tableData.loading = false;
		})
		.catch((err) => {
			console.log(err);
			state.tableData.loading = false;
		});
};
// 打开新增用户弹窗
const onOpenAddDevice = (type: string) => {
	deviceDialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditDevice = (row: any) => {
	deviceEditDialogRef.value.openDialog(row);
};
// 删除用户
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删设备：“${row.code}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			deviceStore
				.deleteDevice(row.id)
				.then(() => {
					getTableData();
					ElMessage.success('删除成功');
				})
				.catch((err) => {
					console.log(err);
					ElMessage.error('删除失败');
				});
		})
		.catch(() => {});
};

const onUnbound = (row: any) => {
	ElMessageBox.confirm(`此操作将解绑设备：“${row.code}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			deviceStore
				.unboundDevice(row.id)
				.then(() => {
					getTableData();
					ElMessage.success('解绑成功');
				})
				.catch((err: any) => {
					console.log(err);
					ElMessage.error(`解绑失败`);
				});
		})
		.catch(() => {});
};
// 查询
const searchList = () => {
	state.tableData.param.pageNum = 1;
	getTableData();
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};

// 页面加载时
onMounted(() => {
	loadDealers();
	getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;

		.el-table {
			flex: 1;
		}
	}
}
</style>
./dealer
